package com.hellojava.util.file;

import com.hellojava.util.StringUtil;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;

import static org.apache.commons.io.FilenameUtils.getExtension;

/**
 * Created by dengj on 2017/9/9.
 */
public class FileUtils {


    private static Logger logger = Logger.getLogger(FileUtils.class);

    @Async
    public static MetaData upload(MultipartFile file) throws IOException {
        MetaData md = new MetaData();
        md.setType(FileType.IMG);
        QiniuFileProvider qiniuFileProvider = new QiniuFileProvider();
        String path = path(getExtension(file.getOriginalFilename()));
        md.setPath(qiniuFileProvider.upload(path, IOUtils.toByteArray(file.getInputStream())));
        md.setSize(getPrintSize(file.getSize()));
        return md;
    }

    public static String uploadByBytes(InputStream is,String suffix) throws IOException {
        QiniuFileProvider qiniuFileProvider = new QiniuFileProvider();
        String path = path(suffix);
        return qiniuFileProvider.upload(path, IOUtils.toByteArray(is));
    }

    /**
     *
     * @param ext 文件名字 (dsadasdasd.apk,或者是23323.jpg)
     * @return
     */
    public static String path(String ext) {
        if(ext == null)
            ext="";
        return new SimpleDateFormat("yyyy/MM/dd/").format(new Date()).concat(StringUtil.uuid() +"."+ ext);
    }

    public static String getPrintSize(long size) {
        //如果字节数少于1024，则直接以B为单位，否则先除于1024，后3位因太少无意义
        if (size < 1024) {
            return String.valueOf(size) + "B";
        } else {
            size = size / 1024;
        }
        //如果原字节数除于1024之后，少于1024，则可以直接以KB作为单位
        //因为还没有到达要使用另一个单位的时候
        //接下去以此类推
        if (size < 1024) {
            return String.valueOf(size) + "KB";
        } else {
            size = size / 1024;
        }
        if (size < 1024) {
            //因为如果以MB为单位的话，要保留最后1位小数，
            //因此，把此数乘以100之后再取余
            size = size * 100;
            return String.valueOf((size / 100)) + "."
                    + String.valueOf((size % 100)) + "MB";
        } else {
            //否则如果要以GB为单位的，先除于1024再作同样的处理
            size = size * 100 / 1024;
            return String.valueOf((size / 100)) + "."
                    + String.valueOf((size % 100)) + "GB";
        }
    }

    public static String getMd5ByFile(MultipartFile file) throws NoSuchAlgorithmException, IOException {
        byte[] uploadBytes = file.getBytes();
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        byte[] digest = md5.digest(uploadBytes);
        String hashString = new BigInteger(1, digest).toString(16);
        return hashString.toUpperCase();
    }

}
